package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTMachineFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.SignalMachineFaultDto;
import com.hualude.roadsign.repository.SignalMachineFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.SignalMachineHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/7/22.
 */
@Service
public class SignalMachineHandleServiceImpl implements SignalMachineHandleService {

    @Resource
    private SignalMachineFaultRepository signalMachineFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getSignalMachine4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception {
        List<DTMachineFaultDto> dto4DTs = signalMachineFaultRepository.getSignalMachine4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        Integer count = signalMachineFaultRepository.getSignalMachineCount4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTMachineFaultDto dtMachineFaultDto:dto4DTs){
            if(dtMachineFaultDto.getMachineMaintainerId()!=null){
                String maintainerName =userRepository.getUserNameByUserId(dtMachineFaultDto.getMachineMaintainerId());
                dtMachineFaultDto.setMachineMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addMachineMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception {
        return signalMachineFaultRepository.addMachineMaintenanceContent(machineFaultId,machineMaintenanceContent);
    }

    @Transactional
    public Integer finishMachineMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId) throws Exception {
        return signalMachineFaultRepository.finishMachineMaintenance(machineMaintenancePhoto,machineMaintainerId,machineMaintenanceTime,machineMaintenanceExplain,machineFaultId);
    }


    public List<SignalMachineFaultDto> getMachineFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return signalMachineFaultRepository.getMachineFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public SignalMachineFaultDto getMachineReceiptInfo4App(Integer machineFaultId) throws Exception{
        return signalMachineFaultRepository.getMachineReceiptInfo4App(machineFaultId);
    }
}
